﻿using ApplicationModels.A02.Identities;
using AutoMapper;
using Commons.Helpers;
using Commons.Responses;
using EntityModels.E02.Identities;
using IApplicationServices;
using IRepositories;

namespace ApplicationService
{
    public class UserService:BaseApplicationService<Guid,AppUser,AppUserAM>,IUserService
    {
        private IUserRepository _repository;
        public UserService(IUserRepository re,IMapper map):base(re,map) { _repository = re; }

        public async Task<MessageResponse> Lockout(Guid id)
        {
            var user = await Repository.GetAsync(id);
            string msg = "封禁成功!";
            if(user != null)
            {
                if (!user.LockoutEnabled)
                {
                    msg = "解禁成功!";
                }
                user.LockoutEnabled = !user.LockoutEnabled;
                await Repository.UpdateAsync(user);
                return new MessageResponse(msg);
            }
            else
            {
                return new MessageResponse("Id错误!",false);
            }
        }

        public async Task<MessageResponse> UpdateAvatar(AppUserAM user)
        {
            var bbo = Mapper.Map<AppUser>(user);
            return await _repository.UpdateAvatar(bbo);
        }
        public async Task<string> GetAvatarByUserIdAsync(Guid userId)
        {
            var user = await Repository.GetAsync(x => x.Id == userId);
            return user == null ? null : user.Avatar.ToString();
        }
    }
}
